<?php

/**
 * @author Martin Pernica
 * @copyright 2008
 */
echo '<br /><h2><a href="index.php?p=components&name=content">'.Lang::getBackGlobal('back').'</a></h2><br />';

if (!empty($_POST['title']) && (!empty($_POST['pretext']) || !empty($_POST['text'])) &&
    !empty($_POST['cat_id'])) {
        $title = Registry::get('db')->real_escape_string(strip_tags($_POST['title']));
        $pretext = Registry::get('db')->real_escape_string($_POST['pretext']);
        $text = Registry::get('db')->real_escape_string($_POST['text']);
        $tags = strip_tags(Registry::get('db')->real_escape_string($_POST['tags']));
        $cat_id = intval($_POST['cat_id']);
        $closed = ($_POST['closed'] == 1) ? '1' : '0';
        $published = ($_POST['published'] == 1) ? '1' : '0';
        $frontpage = ($_POST['frontpage'] == 1) ? '1' : '0';
        $notice = ($_POST['notice'] == 1) ? '1' : '0';

        $query = Registry::get('db')->query("INSERT INTO {prefix}articles (title, pretext, text, tags, author_id, cat_id, closed, published, date) VALUES('" .
            $title . "', '" . $pretext . "', '" . $text . "', '" . $tags . "', '" . User::getUid() .
            "', '" . $cat_id . "', '" . $closed . "', '" . $published . "', NOW())");
        $log->write(8, 'com_content', 'NOW()', 'User created new article', $_SESSION['id']);
        messageBox( Lang::getBack('com_content', 'added_done') );
}

$sections = Registry::get('db')->query("SELECT id, name FROM {prefix}sections");

$sections_select = array();

while ($sec = $sections->fetch_array()) {
    $categories = Registry::get('db')->query("SELECT id, name, section FROM {prefix}categories WHERE section = '" . $sec['id'] . "'");
    $sections_select[$sec['name']] = array();
    while ($cats = $categories->fetch_array()) {
        $sections_select[$sec['name']][$cats['name']] = $cats['id'];
    }
}

$form = new Form;
$form->addTextbox('title', Lang::getBack('com_content', 'title'));
$form->addTextarea('pretext', Lang::getBack('com_content', 'pretext'), null, 30, 80, 'wysiwyg');
$form->addTextarea('text', Lang::getBack('com_content', 'text'), null, 30, 80, 'wysiwyg');
$form->addTextarea('tags', Lang::getBack('com_content', 'tags'), null, 5, 30);
$form->addSelect('cat_id', Lang::getBack('com_content', 'category'), $sections_select);
$form->addCheckbox('frontpage', Lang::getBack('com_content', 'frontpage'), 1);
$form->addCheckbox('notice', Lang::getBack('com_content', 'notice'), 1);
$form->addCheckbox('closed', Lang::getBack('com_content', 'closed'), 1);
$form->addCheckbox('published', Lang::getBack('com_content', 'published'), 1);
$form->addSubmit('submit_edit', Lang::getBackGlobal('submit'));

echo $form;

?>